/***************************************************************************
 *   Copyright (C) 2006 by Sergejs Melderis   *
 *   sergey.melderis@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef BROWSER_H
#define BROWSER_H
#include "browserpopupmenu.h"
#include <kurl.h>
#include <khtml_part.h>
#include <qframe.h>
#include <qpopupmenu.h>
#include <khtml_events.h>
class QResizeEvent;
class QString;
class QKeyEvent;
class QLabel;
class QTimer;
class Tooltip;
/**
This class implements a simple browser for KLingvoTranslator using KHTMLPart.

    @author Sergejs Melderis <sergey.melderis@gmail.com>
*/
class BrowserImpl : public KHTMLPart
{
    Q_OBJECT
public:
    BrowserImpl(QWidget *parent = 0, const char *name = 0);

    ~BrowserImpl();

    KURL mURL;

    /**
     * This function is called when error occurs.
     */
    void showError(KIO::Job *job);
signals:
    void wordSelected(const QString &word, const QPoint &p);

protected:
    void khtmlMousePressEvent(khtml::MousePressEvent *event);


    //void khtmlMouseMoveEvent(khtml::MouseMoveEvent *event);
private slots:
    /**
     * This slot will be called when users clicks on the link on the page.
     * Slot will open the page only if the path of url is the same as the
     * path of current url. Use your favourite browser if you want to browse the web.
     * @param url the url to open.
     */
    void openRequestedURL(const KURL &url);

    /**
     * In the case of error this slot will show an error message.
     * @param @errStr the error message.
     */
    void showErrorMessage(const QString& errStr);
};

/**
 * This class wraps KHTMLPart in its own frame. The purpose it to
 * reimplement some protected events.
 */
class Browser : public QFrame
{
    Q_OBJECT
public:
    Browser(QWidget *parent = 0, BrowserPopupMenu *menu = 0);

    ~Browser();
    /**
     * Set the only url the browser will use.
     * @param url the url.
     */
    void setURL(const KURL &url);

    KURL url()const;

    /**
     * Set query to append to url.
     * @param query the query.
     */
    void openPageForQuery(const QString &query);

    KHTMLPart *htmlPart()const;

public slots:
    void showAddWordResult(const QString &result);

signals:
    void closed();

private:
    BrowserImpl *b;
    void writeContactingLingvo();
    void init();

    BrowserPopupMenu *mMenu;

    Tooltip *m_tooltip;
    QPoint lastPoint;
    QTimer *labelTimer;

private slots:
    void showMenu(const QString &word, const QPoint &point);

    

protected:
    /**
     * Reimplemented from QFrame. If it is a Escape key it will
     * close the widget.
     * @param e the key event.
     */
    void keyPressEvent(QKeyEvent *e);

    /**
     * Reimplemented from QFrame. Used to store size in settigs.
     * @param e the resize event.
     */
    void resizeEvent(QResizeEvent *e);

    /**
     * Reimplemented from QFrame.
     * We want to write a message to the browser so when next time is
     * used it will be there.
     * @param e the close event.
     */
    void closeEvent(QCloseEvent *e);

};

#endif
